/**
 * Created by root on 17-7-29.
 */

var index = require('../index');
var logger = require('../config/log4js');

module.exports = {

    addIP : function (IP, callback) {
        return new Promise(function (resolve, reject) {
            try {

                //获取数据源
                var dbPool = index.getDB();

                dbPool.getConnection(function(err, connection) {
                    // Use the connection
                    connection.query('INSERT INTO ip_list (ip, port, type, country, province, city, status, create_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', [IP.ip, IP.port, IP.type, IP.country, IP.province, IP.city, IP.status, IP.createDate], function (error, results, fields) {
                        // And done with the connection.
                        connection.release();

                        // Handle error after the release.
                        if (error) throw error;

                        // Don't use the connection here, it has been returned to the pool.
                        logger.access.info(results);
                        callback(results);

                        resolve(results);
                    });
                });

            } catch (e) {
                logger.error.info(e);
                reject(e.toString());
            }
        });
    },

    queryIP : function (ip, port, callback) {

        return new Promise(function (resolve, reject) {

            try {

                //获取数据源
                var dbPool = index.getDB();

                dbPool.getConnection(function(err, connection) {
                    // Use the connection
                    connection.query('SELECT * FROM ip_list WHERE ip = ? AND port = ?', [ip, port], function (error, results, fields) {
                        // And done with the connection.
                        connection.release();

                        // Handle error after the release.
                        if (error) throw error;

                        // Don't use the connection here, it has been returned to the pool.
                        logger.access.info(results);
                        callback(results);

                        resolve(results);
                    });
                });

            } catch (e) {
                logger.error.info(e);
                reject(e.toString());
            }

        });


    },

    queryIPByStatus : function (status, callback) {

        return new Promise(function (resolve, reject) {

            try {

                //获取数据源
                var dbPool = index.getDB();

                dbPool.getConnection(function(err, connection) {
                    // Use the connection
                    connection.query('SELECT * FROM ip_list WHERE status = ?', [status], function (error, results, fields) {
                        // And done with the connection.
                        connection.release();

                        // Handle error after the release.
                        if (error) throw error;

                        // Don't use the connection here, it has been returned to the pool.
                        logger.access.info(results);
                        callback(results);

                        resolve(results);
                    });
                });

            } catch (e) {
                logger.error.info(e);
                reject(e.toString());
            }

        });

    },

    updateByIPAndPort : function (IP, callback) {

        return new Promise(function (resolve, reject) {

            try {

                //获取数据源
                var dbPool = index.getDB();

                dbPool.getConnection(function(err, connection) {
                    // Use the connection
                    connection.query('UPDATE ip_list SET status = ?, validate_count = ? WHERE ip = ? AND port = ?', [IP.status, IP.validateCount, IP.ip, IP.port], function (error, results, fields) {
                        // And done with the connection.
                        connection.release();

                        // Handle error after the release.
                        if (error) throw error;

                        // Don't use the connection here, it has been returned to the pool.
                        logger.access.info(results);
                        callback(results);

                        resolve(results);
                    });
                });

            } catch (e) {
                logger.error.info(e);
                reject(e.toString());
            }

        });

    },

    queryIPBySize(size, callback) {
        return new Promise(function (resolve, reject) {
            try {

                //获取数据源
                var dbPool = index.getDB();

                dbPool.getConnection(function(err, connection) {
                    if (err) throw err;
                    // Use the connection
                    connection.query('SELECT * FROM ip_list ORDER BY create_date DESC LIMIT 0,?', [size], function (error, results, fields) {
                        // And done with the connection.
                        connection.release();

                        // Handle error after the release.
                        if (error) throw error;

                        // Don't use the connection here, it has been returned to the pool.
                        logger.access.info(results);
                        callback(results);

                        resolve(results);
                    });
                });

            } catch (e) {
                logger.error.info(e);
                reject(e.toString());
            }
        });
    }

};
